package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.bean.RepairRecord;
import com.mxnet.chongjin.supoman.config.Api;

public class RepairOrderAcceptedActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout, mLayout;
    private RepairRecord mRepairRecord;
    private Button mButton;
    private TextView mTvOrderNum, mTvXulieNum, mTvMachineInfo, mTvPartnerName, mTvPartnerTel, mTvPartnerAddr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_order_accepted);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = RepairOrderAcceptedActivity.this;
        Bundle bundle = getIntent().getExtras();
        mRepairRecord = bundle.getParcelable("record");

        initView();
        initData();
    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_repair_order_accepted);
        mLinearLayout.setOnClickListener(this);

        mLayout = findViewById(R.id.ll_repair_order_accepted_2);

        mButton = findViewById(R.id.btn_repair_order_accepted);
        mButton.setOnClickListener(this);

        mTvOrderNum = findViewById(R.id.tv_repair_order_accepted_order_num);
        mTvOrderNum.setText(getResources().getString(R.string.repair_num) + mRepairRecord.getDeclarationNo());

        mTvXulieNum = findViewById(R.id.tv_repair_order_accepted_xulie_num);
        mTvXulieNum.setText(getResources().getString(R.string.serial_number) + mRepairRecord.getProjectNo());

        mTvMachineInfo = findViewById(R.id.tv_repair_order_accepted_machine_info);
        mTvMachineInfo.setText(mRepairRecord.getDeclarationMemo());

        mTvPartnerName = findViewById(R.id.tv_repair_order_accepted_partner_name);
        mTvPartnerName.setText(getResources().getString(R.string.partner_name) + mRepairRecord.getCustomerName());

        mTvPartnerTel = findViewById(R.id.tv_repair_order_accepted_partner_tel);
        mTvPartnerTel.setText(getResources().getString(R.string.contact) + mRepairRecord.getCustomerMobile());

        mTvPartnerAddr = findViewById(R.id.tv_repair_order_accepted_partner_addr);
        mTvPartnerAddr.setText(getResources().getString(R.string.contact_addr) + mRepairRecord.getCountry() + mRepairRecord.getCity() + mRepairRecord.getDistirct() + mRepairRecord.getDetailAddress());
    }

    public void initData() {
        for (int i = 0; i < mRepairRecord.getResourceIdList().size(); i++) {
            ImageView imageView = new ImageView(mContext);
            Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mRepairRecord.getResourceIdList().get(i)).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(imageView);
            mLayout.addView(imageView);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_repair_order_accepted:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.btn_repair_order_accepted:
                Intent intent = new Intent();
                intent.setClass(mContext, RepairRecordDefaultActivity.class);
                intent.putExtra("recordNo", mRepairRecord.getDeclarationNo());
                intent.putExtra("machineNo", mRepairRecord.getProjectNo());
                mContext.startActivity(intent);
                break;
        }
    }
}
